
class snow{
    constructor(data){
        this.width=data.width || 1280
        this.height=data.height || 1080
        this.count=  data.count || 20
        this.minSize=17
        this.maxSize=data.maxSize-this.minSize|| 21
        this.box=data.box
        
   }
   //生成一片雪花
   creatSnow(){
     for(let i=0;i<this.count;i+=1){
         let node= this.creatNode() 
         this.box.appendChild(node)      
     }
   }
   star(){
    this.creatSnow()
    for(let i=0;i<this.box.children.length;i+=1){
        this.move(this.box.children[i])    
    }
   }
   end(){
      for(let i=this.box.children.length-1;i>=0;i--){
        clearInterval(this.box.children[i].timer)  //清楚定时器
        this.box.removeChild(this.box.children[i])
      } 
   }
   //生成雪花结点
   creatNode(){
     let x =parseInt(Math.random()*this.width)
     let y=parseInt(Math.random()*this.height )  

     let node=document.createElement("IMG");
     node.src=require("@/assets/default/information/Snow.png")

     node.style.position='absolute'
     node.style.top=y+'px'
     node.style.left=x+'px'
     
     node=this.refresh(node)

     return node
   }
   refresh(node){

    let size=parseInt(Math.random()*this.maxSize+this.minSize) //大小
    node.style.width=size+'px'  
    node.style.height=size+'px' 


    node.vx=parseInt((Math.random()*8))+1 //移动速度
    node.vy= node.vx //移动速度
    let time =parseInt (1 / node.vx * 3) +1
    node.style.animation=`rotate linear ${time}s infinite`
    return node
   }

   //移动
   move(node){
     let top=0
     let left=0
     node.timer = setInterval(() => {
        top=parseInt(node.style.top)
        left=parseInt(node.style.left)
        let newTop=(top+node.vy) 
        let newLeft=(left +node.vx) 
        node.style.top=newTop+'px'
        node.style.left=newLeft+'px'
        if(newTop >=this.height || newLeft >=this.width){
            node.style.top=newTop%this.height+'px'
            node.style.left=newLeft%this.width+'px'
            node=this.refresh(node)
        }
        
     }, 10);
   }
}
export default snow